package net.sansi.v3correctserver.service.show.impl;

import net.sansi.v3correctserver.constenum.AppHelper;
import net.sansi.v3correctserver.dto.v3serverapi.V3ServerCorrectDto;
import net.sansi.v3correctserver.entity.screen.CorrectArea;
import net.sansi.v3correctserver.entity.space.CorrectCnf;
import net.sansi.v3correctserver.service.show.HardwareShowService;
import net.sansi.v3correctserver.service.show.ShowService;
import net.sansi.v3correctserver.service.v3serverapi.V3ServerCorrectApi;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * Created by Fang
 * create time : 2025/2/11
 * IDE : IntelliJ IDEA
 * <p>
 * 硬件打屏
 */

@Service
public class HardwareShowServiceImpl implements HardwareShowService {

    @Resource
    private V3ServerCorrectApi v3ServerCorrectApi;


    @Override
    public V3ServerCorrectDto.HardwarePreviewEnable hardwarePreviewEnable(String screenId) {
        return v3ServerCorrectApi.enableHardwarePreview(screenId);
    }

    /**
     * 打屏
     *
     * @param area     区域
     * @param bright   亮度
     * @param colorIdx 颜色索引
     * @param span     隔点
     */
    @Override
    public ShowService.ShowClose show(CorrectArea area, int bright, int colorIdx, int span, int ofx, int ofy) {
        CorrectCnf correctCnf = AppHelper.projectSpace().getCorrectCnf();
        V3ServerCorrectDto.HardwarePreviewDisplayReq req = new V3ServerCorrectDto.HardwarePreviewDisplayReq();

        V3ServerCorrectDto.HardwarePreviewDisplayReq.Color color = new V3ServerCorrectDto.HardwarePreviewDisplayReq.Color();
        V3ServerCorrectDto.HardwarePreviewDisplayReq.AreaInfo areaInfo = new V3ServerCorrectDto.HardwarePreviewDisplayReq.AreaInfo();
        V3ServerCorrectDto.HardwarePreviewDisplayReq.SplitInfo splitInfo = new V3ServerCorrectDto.HardwarePreviewDisplayReq.SplitInfo();

        if (colorIdx == 0) color.setRed(bright);
        else if (colorIdx == 1) color.setGreen(bright);
        else if (colorIdx == 2) color.setBlue(bright);
        else if (colorIdx == 3) {
            color.setRed(bright);
            color.setGreen(bright);
            color.setBlue(bright);
        }

        areaInfo.setXStart(area.getShowArea().getStart().getX());
        areaInfo.setYStart(area.getShowArea().getStart().getY());
        areaInfo.setXShowW(area.getShowArea().getWidth());
        areaInfo.setYShowH(area.getShowArea().getHeight());

        splitInfo.setXSplitNum(span);
        splitInfo.setYSplitNum(span);
        splitInfo.setXOffset(ofx);
        splitInfo.setYOffset(ofy);

        req.setColor(color);
        req.setAreaInfo(areaInfo);
        req.setSplitInfo(splitInfo);

        V3ServerCorrectDto.HardwarePreviewResp resp = v3ServerCorrectApi.displayHardwarePreview(correctCnf.getScreenId(), req);
        // TODO: 向上传递打屏结果
        return this::close;
    }

    @Override
    public void close() {
        CorrectCnf correctCnf = AppHelper.projectSpace().getCorrectCnf();
        V3ServerCorrectDto.HardwarePreviewResp resp = v3ServerCorrectApi.clearHardwarePreview(correctCnf.getScreenId());
        // TODO: 向上传递清楚打屏结果
    }
}
